package com.itshidu.jiaowu.controller;


import com.itshidu.jiaowu.dao.TeacherMapper;
import com.itshidu.jiaowu.dao.UserMapper;
import com.itshidu.jiaowu.entity.Teacher;
import com.itshidu.jiaowu.entity.User;
import com.itshidu.jiaowu.service.TeacherService;
import com.itshidu.jiaowu.util.LoginUtil;
import com.itshidu.jiaowu.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XiaZengming123
 * @since 2019-07-08
 */
@Controller
public class TeacherAction {
    @Autowired TeacherService teacherservice;
    @Autowired UserMapper usermapper;
    @Autowired TeacherMapper teachermapper;

    //前端传递的时间类型与后台不一致，使用该方法，否则报错
    @InitBinder
    public void init(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    //查询老师个人信息
    @ResponseBody
    @RequestMapping("/admin/teacher/select")
    public Object TeacherInfo(ModelAndView mv){
        User u=(User) LoginUtil.get("UserInfo");
        User user=usermapper.selectById(u.getId());
        Teacher teacher=teachermapper.selectById(u.getId());
        mv.addObject("aaa",user);
        mv.addObject("ccc",teacher);
        LoginUtil.set("Teacher",teacher);
        mv.setViewName("TeacherPersonalInfo");
        return mv;
    }


    //修改老师信息
    @ResponseBody
    @RequestMapping("/admin/teacher/update")
    public Result update(Teacher teacher,User user) {
        return teacherservice.updated(teacher,user);
    }

    //老师修改密码
    @ResponseBody
    @RequestMapping("/admin/teacher/password")
    public Result ChangePwd(String oldpwd,String newpwd1,String newpwd2) {
        return teacherservice.ChangePwd(oldpwd,newpwd1,newpwd2);
    }






}

